<script lang="ts" setup>
import { ref,h } from 'vue';
import { useVbenDrawer } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { detailApi, editApi } from '#/api/system/setting';

const key = ref('');

const [Form, formApi] = useVbenForm({
  schema: [
    {
      component: 'Divider',
      fieldName: '',
      hideLabel: true,
      renderComponentContent: () => {
        return {
          default: () => h('div', '微信支付'),
        };
      },
    },
    {
        fieldName: 'wechat.version',
        label: 'version',
        component: 'Input',
        defaultValue: 'v3',
        dependencies: {
            show: false,
            triggerFields: [''],
        },
    },
    {
        fieldName: 'wechat.mchType',
        label: 'mchType',
        component: 'Input',
        defaultValue: 'normal',
        dependencies: {
            show: false,
            triggerFields: [''],
        },
    },
    {
        fieldName: 'wechat.appId',
        label: '应用ID (AppID)',
        component: 'Input',
        labelWidth: 140,
    },
    {
        fieldName: 'wechat.mchId',
        label: '微信商户号 (MchId)',
        component: 'Input',
        labelWidth: 140,
    },
    {
        fieldName: 'wechat.apiKey',
        label: '支付密钥 (APIKEY)',
        component: 'Input',
        labelWidth: 140,
    },
    {
        fieldName: 'wechat.apiclientCert',
        label: '证书文件 (CERT)',
        component: 'FileUpload',
        labelWidth: 140,
        componentProps: {
          maxCount: 1,
          engine: 'local'
        },
        formItemClass: 'items-start',
        labelClass: 'pt-1',
    },
    {
        fieldName: 'wechat.apiclientKey',
        label: '证书文件 (KEY)',
        component: 'FileUpload',
        labelWidth: 140,
        componentProps: {
          maxCount: 1,
          engine: 'local'
        },
        formItemClass: 'items-start',
        labelClass: 'pt-1',
    },
    {
        fieldName: 'wechat.platformCert',
        label: '平台证书',
        component: 'FileUpload',
        labelWidth: 140,
        componentProps: {
          maxCount: 1,
          engine: 'local'
        },
        formItemClass: 'items-start',
        labelClass: 'pt-1',
    },
    {
      component: 'Divider',
      fieldName: '',
      hideLabel: true,
      renderComponentContent: () => {
        return {
          default: () => h('div', '支付宝'),
        };
      },
    },
    {
        fieldName: 'alipay.appId',
        label: '支付宝应用 (AppID)',
        component: 'Input',
        labelWidth: 160,
    },
    {
        fieldName: 'alipay.signType',
        label: '签名算法 (signType)',
        component: 'RadioGroup',
        labelWidth: 160,
        defaultValue: 10,
        componentProps: {
        options: [
            { label: 'RSA2', value: 10 },
        ]
        },
        rules: 'required',
    },
    {
        fieldName: 'alipay.signMode',
        label: '加签模式',
        component: 'RadioGroup',
        labelWidth: 160,
        defaultValue: 10,
        componentProps: {
        options: [
            { label: '公钥证书', value: 10 },
            { label: '公钥', value: 20 },
        ]
        },
        rules: 'required',
    },
    {
        fieldName: 'alipay.alipayPublicKey',
        label: '支付宝公钥',
        component: 'Textarea',
        labelWidth: 160,
        dependencies: {
            show(values) {
                return values.alipay.signMode === 20;
            },
            triggerFields: ['alipay'],
        },
    },
    {
        fieldName: 'alipay.appCertPublicKey',
        label: '应用公钥证书',
        component: 'FileUpload',
        labelWidth: 160,
        componentProps: {
          maxCount: 1,
          engine: 'local'
        },
        formItemClass: 'items-start',
        labelClass: 'pt-1',
        dependencies: {
            show(values) {
                return values.alipay.signMode === 10;
            },
            triggerFields: ['alipay'],
        },
    },
    {
        fieldName: 'alipay.alipayCertPublicKey',
        label: '支付宝公钥证书',
        component: 'FileUpload',
        labelWidth: 160,
        componentProps: {
          maxCount: 1,
          engine: 'local'
        },
        formItemClass: 'items-start',
        labelClass: 'pt-1',
        dependencies: {
            show(values) {
                return values.alipay.signMode === 10;
            },
            triggerFields: ['alipay'],
        },
    },
    {
        fieldName: 'alipay.alipayRootCert',
        label: '支付宝根证书',
        component: 'FileUpload',
        labelWidth: 160,
        componentProps: {
          maxCount: 1,
          engine: 'local'
        },
        formItemClass: 'items-start',
        labelClass: 'pt-1',
        dependencies: {
            show(values) {
                return values.alipay.signMode === 10;
            },
            triggerFields: ['alipay'],
        },
    },
    {
        fieldName: 'alipay.merchantPrivateKey',
        label: '应用私钥',
        component: 'Textarea',
        labelWidth: 160,
    },
  ],
  showDefaultActions: false,
});

const [Drawer, drawerApi] = useVbenDrawer({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) return;
    const values = await formApi.getValues();
    drawerApi.lock();
    editApi(key.value, values)
      .then((res) => {
        message.success({
          content: res.message,
        });
        drawerApi.close();
      })
      .catch(() => {
        drawerApi.unlock();
      });
  },
  onOpenChange(isOpen) {
    if (isOpen) {
      const data = drawerApi.getData();
      formApi.resetForm();
      if (data.key) {
        key.value = data.key;
        drawerApi.setState({ title: data.describe })
        detailApi(data.key).then((res) => {
          formApi.setValues(res.values);
        })
      }
    }
  },
  overlayBlur: 1,
  title: '',
//   class: 'w-[380px]',
});
</script>
<template>
  <Drawer>
    <Form />
  </Drawer>
</template>
